package com.ys.controller.api.hr;

import com.ys.config.MenuId;
import com.ys.entity.hr.TbProvidentFund;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.hr.TbProvidentFundService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tbProvidentFund")
public class ApiInterTbProvidentFundController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<TbProvidentFund> commonApiService;
	@Autowired
	private TbProvidentFundService tbProvidentFundService;
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTbProvidentFundController.class);
//	@RequiresPermissions("tpFbaPlanOrder:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute TbProvidentFund cond
			){
		cond.setOrderby("A.Id DESC");

		return commonApiService.searchLay(cond);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/toAdd")
	@ResponseBody
	public R add(HttpServletRequest request, @RequestParam Map<String, Object> params) {
		try {
			logger.info("公积金管理新增传参：" + params);
			//修改
			return tbProvidentFundService.add(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("tbProvidentFund:edit")
	@PostMapping("/toEdit")
	@ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tbProvidentFundService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}


	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TbProvidentFund cond){
		//cond.addField("A.PfUnitMonth AS providentFundByCompany")//公积金（单位）/月
				//.addField("A.PfIndividualMonth AS providentFundByPersonal");//公积金（个人）/月
		cond.setStatus(1);
		return commonApiService.searchLay(cond);
	}
	/*
	 * 导出
	 */
	@RequiresPermissions("tbProvidentFund:export")
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TbProvidentFund cond) throws Exception{
		cond.setOrderby("A.Id DESC");

		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();

		List<YsCol> listField = new YsCol()
				.setMenuId(MenuId.TB_ProvidentFund)
				.setFrontShow(YsColEnum.FrontShow.SHOW.value)
				.setOrderby("A.SortNo asc")
				.queryList();


		ExportExcelUtils<TbProvidentFund> util = new ExportExcelUtils<TbProvidentFund>(new TbProvidentFund());

		String toFilePath = util.exportExcel(listData, listField, "公积金管理");
		UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
		return R.ok(dto.getUrl());
	}
}
